package com._520it.member.service.impl;

import com._520it.member.domain.SystemMenu;
import com._520it.member.mapper.SystemMenuMapper;
import com._520it.member.page.PageResult;
import com._520it.member.query.QueryObject;
import com._520it.member.service.ISystemMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class SystemMenuServiceImpl implements ISystemMenuService {
	@Autowired
	private SystemMenuMapper systemMenuMapper;
	
	public int deleteByPrimaryKey(Long id) {
		return systemMenuMapper.deleteByPrimaryKey(id);
	}

	public int insert(SystemMenu record) {
		return systemMenuMapper.insert(record);
	}

	public SystemMenu selectByPrimaryKey(Long id) {
		return systemMenuMapper.selectByPrimaryKey(id);
	}

	public List<SystemMenu> selectAll() {
		return systemMenuMapper.selectAll();
	}

	public int updateByPrimaryKey(SystemMenu record) {
		return systemMenuMapper.updateByPrimaryKey(record);
	}

	@Override
	public PageResult queryPage(QueryObject qo) {
		int count = systemMenuMapper.queryPageCount(qo);
		if(count<=0){
			return new PageResult(0, Collections.EMPTY_LIST);
		}
		List<SystemMenu> result = systemMenuMapper.queryPageDataResult(qo);
		PageResult pageResult = new PageResult(count,result);
		return pageResult;
	}

	@Override
	public List<SystemMenu> queryTree() {
		return systemMenuMapper.queryTree();
	}

	@Override
	public List<SystemMenu> queryForRole() {
		return systemMenuMapper.queryTree();
	}

	@Override
	public List<Long> queryMenuIdsListForRole(Long roleId) {
		return systemMenuMapper.systemMenuMapper(roleId);
	}

	@Override
	public List<SystemMenu> indexMenu() {
		List<SystemMenu> userMenus = systemMenuMapper.queryTree();
		return userMenus;

	}
	private void filterMenu(List<SystemMenu> userMenus, List<Long> ids) {
		SystemMenu menu = null;
		for (int i = userMenus.size()-1; i >=0 ; i--) {
			menu = userMenus.get(i);
			if (!ids.contains(menu.getId())) {
				userMenus.remove(i);
			}else{
				if (menu.getChildren() != null && menu.getChildren().size() > 0) {
					filterMenu(menu.getChildren(),ids);
				}
			}
		}
	}
}
